<?php
namespace app\providers\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;

// 命令  php think link

class LinkCommand extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('link');
        // 设置参数
        $this->setDescription('设置软链接');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->link( root_path('views') , public_path('admins').'views' );
        // $this->link( root_path('runtime').'storage/uploads' , public_path().'uploads' );
        // $this->link( storage_path('app/public'), public_path('storage') );
        // 指令输出
        $output->writeln('完成');
    }
    
    
    public function link($target, $link)
    {
        if ( strtolower(substr(PHP_OS, 0, 3)) === 'win' ) {
            $mode = $this->isDirectory($target) ? 'J' : 'H';
               exec("mklink /{$mode} ".escapeshellarg($link).' '.escapeshellarg($target));
       }else{
           return symlink($target, $link);
       }
    }
    
    public function isDirectory($directory)
    {
        return is_dir($directory);
    }

}
